package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfSignNo;
import com.ocom.common.entity.payconfig.EntityXfSignOrder;
import com.ocom.common.entity.typehandler.SignNoHandle;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfSignOrder;
import com.ocom.payconfig.service.XfAccountInfoService;
import com.ocom.payconfig.service.XfSignNoService;
import com.ocom.payconfig.service.XfSignOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class XfSignOrderServiceImpl extends ServiceImpl<IDaoXfSignOrder, EntityXfSignOrder> implements XfSignOrderService {


    @Autowired
    XfAccountInfoService xfAccountInfoService;

    @Autowired
    XfSignNoService xfSignNoService;

    @Autowired
    IDaoXfSignOrder iDaoXfSignOrder;

    private  static  String key = "6iQq6ZyiCQ4ue3WFbeCTTlFYLpzLwSMG";

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityXfSignOrder addSignOrder(Long companyId,EntityXfSignOrder entityXfSignOrder) {
        if(!CommonUtil.isNull(entityXfSignOrder.getBank())){
            entityXfSignOrder.setBank(iDaoXfSignOrder.encode(entityXfSignOrder.getBank() ,key));
        }
        Boolean b = this.save(entityXfSignOrder);
        if(b){
            return entityXfSignOrder;
        }
        return null;
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateSignOrder(Long companyId,EntityXfSignOrder entityXfSignOrder) {
        UpdateWrapper<EntityXfSignOrder> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id",entityXfSignOrder.getComId());
        updateWrapper.eq("order_no",entityXfSignOrder.getOrderNo());
        updateWrapper.eq("status",1);
        updateWrapper.set("status",entityXfSignOrder.getStatus());
        updateWrapper.set("sign_no",entityXfSignOrder.getSignNo());
        if(entityXfSignOrder.getPayWay() == 37){
            log.debug("签约号："+entityXfSignOrder.getSignNo());
            SignNoHandle signNoHandle = new SignNoHandle();
            String sign = signNoHandle.encrypt(entityXfSignOrder.getSignNo(), 1l);
            updateWrapper.set("sign_no",sign);
        }
        Boolean b = this.update(updateWrapper);
        if(!b){
            log.error("更新失败");
            return false;
        }
        //成功的话
        if(entityXfSignOrder.getStatus()==2){
            QueryWrapper<EntityXfSignOrder> queryWrapper = Wrappers.query();
            queryWrapper.eq("com_id",entityXfSignOrder.getComId());
            queryWrapper.eq("order_no",entityXfSignOrder.getOrderNo());
            EntityXfSignOrder entityXfSignOrder1 = this.getOne(queryWrapper);



            EntityXfSignNo entityXfSignNo =new EntityXfSignNo();
            entityXfSignNo.setComId(entityXfSignOrder.getComId());
            entityXfSignNo.setMerchId(entityXfSignOrder.getMerchId());
            entityXfSignNo.setPId(entityXfSignOrder.getPId());
            entityXfSignNo.setPayWay(entityXfSignOrder.getPayWay());
            entityXfSignNo.setSignNo(entityXfSignOrder.getSignNo());

            //更新人员的签约
            //签约
            if(entityXfSignOrder.getSignType()==1){
//                if(entityXfSignOrder.getMerchId()==0){
//                    xfAccountInfoService.sign(entityXfSignOrder.getComId(),entityXfSignOrder1.getPId(),
//                            entityXfSignOrder.getPayWay(),entityXfSignOrder.getSignNo());
//                }

                xfSignNoService.addSignNo(entityXfSignOrder.getComId(),entityXfSignNo);
            }else{
                //解约
//                if(entityXfSignOrder.getMerchId()==0){
//                    xfAccountInfoService.sign(entityXfSignOrder.getComId(),entityXfSignOrder1.getPId(),
//                            0,"");
//                }
                xfSignNoService.delSignNo(entityXfSignOrder.getComId(),entityXfSignNo);
            }
        }
        return true;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfSignOrder getSignOrder(Long companyId,Integer merchId, String orderNo) {
        QueryWrapper<EntityXfSignOrder> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",companyId);
        if(merchId!=null){
            queryWrapper.eq("merch_id",merchId);
        }
        queryWrapper.eq("order_no",orderNo);
        EntityXfSignOrder entityXfSignOrder = this.getOne(queryWrapper);
        if(entityXfSignOrder!=null){
            entityXfSignOrder.setBank(decode(companyId,entityXfSignOrder.getBank(),key));
        }
        return  entityXfSignOrder;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfSignOrder> getUserSignPhones(Long companyId, List<Long> pIds) {
        QueryWrapper<EntityXfSignOrder> queryWrapper = Wrappers.query();
//        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("sign_type", 1);
        queryWrapper.eq("status", 2);
        queryWrapper.in("p_id", pIds);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.groupBy("p_id");

        List<EntityXfSignOrder> xfSignOrderList = this.list(queryWrapper);

        for (EntityXfSignOrder entityXfSignOrder1:xfSignOrderList){
            if(CommonUtil.isNull(entityXfSignOrder1.getBank()))  continue;
            try {
                entityXfSignOrder1.setBank(decode(companyId,entityXfSignOrder1.getBank(),key));
            }catch (Exception e){

            }
        }
        return xfSignOrderList;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfSignOrder> list(Long companyId, EntityXfSignOrder entityXfSignOrder) {
        List<EntityXfSignOrder> xfSignOrderList =  this.list(Wrappers.lambdaQuery(EntityXfSignOrder.class)
                .eq(EntityXfSignOrder::getComId,entityXfSignOrder.getComId())
                .eq(EntityXfSignOrder::getPId,entityXfSignOrder.getPId())
                .orderByDesc(EntityXfSignOrder::getCreateTime)
        );
        for (EntityXfSignOrder entityXfSignOrder1:xfSignOrderList){
            if(CommonUtil.isNull(entityXfSignOrder1.getBank()))  continue;
            try {
                entityXfSignOrder1.setBank(decode(companyId,entityXfSignOrder1.getBank(),key));
            }catch (Exception e){

            }
        }
        return xfSignOrderList;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public String encode(Long companyId, String phone, String key) {
        return iDaoXfSignOrder.encode(phone,key);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public String decode(Long companyId, String phone, String key) {
        return iDaoXfSignOrder.decode(phone,key);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Page<EntityXfSignOrder> list(Long companyId, int page, int size) {
        Page<EntityXfSignOrder> page1 =  this.page(new Page<>(page,size));

        if(page1.getRecords().size()>0){
            for (EntityXfSignOrder entityXfSignOrder1:page1.getRecords()){
                if(CommonUtil.isNull(entityXfSignOrder1.getBank()))  continue;
                try {
                    entityXfSignOrder1.setBank(decode(companyId,entityXfSignOrder1.getBank(),key));
                }catch (Exception e){

                }
            }
        }
        return  page1;
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public boolean updatePhone(Long companyId, EntityXfSignOrder entityXfSignOrder) {
        return this.updateById(entityXfSignOrder);
    }

}
